Conditions | 10 |
Total Lines | 91 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like StorageRbacService.parseGrants often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import { Inject, Injectable, Optional } from '@nestjs/common'; |
||
56 | |||
57 | private async parseGrants(): Promise<object> { |
||
58 | |||
59 | if (this.cache) { |
||
60 | const cache = await this.getFromCache(); |
||
61 | if (cache) { |
||
62 | |||
63 | return cache; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | const {grants, permissions} = await this.rbac.getRbac(); |
||
68 | const result = {}; |
||
69 | Object.keys(grants).forEach((key) => { |
||
70 | const grant = grants[key]; |
||
71 | |||
72 | result[key] = [ |
||
73 | // remove duplicate |
||
74 | ...new Set( |
||
75 | // get extended |
||
76 | grant.filter((value: string) => !value.startsWith('&')), |
||
77 | ), |
||
78 | ] |
||
79 | // remove not existed |
||
80 | .filter((value: string) => { |
||
81 | if (value.includes('@')) { |
||
82 | const spilt = value.split('@'); |
||
83 | if (!permissions[spilt[0]]) { |
||
84 | return false; |
||
85 | } |
||
86 | |||
87 | return permissions[spilt[0]].some((inAction) => inAction === spilt[1]); |
||
88 | } |
||
89 | if (permissions[value]) { |
||
90 | return permissions[value]; |
||
91 | } |
||
92 | |||
93 | }); |
||
94 | |||
95 | }); |
||
96 | const findExtendedGrants = {}; |
||
97 | Object.keys(grants).forEach((key) => { |
||
98 | const grant = grants[key]; |
||
99 | |||
100 | findExtendedGrants[key] = [ |
||
101 | // remove duplicate |
||
102 | ...new Set( |
||
103 | // get extended |
||
104 | grant.filter((value: string) => { |
||
105 | if (value.startsWith('&')) { |
||
106 | const subGrant = value.substr(1); |
||
107 | if (grants[value.substr(1)] && subGrant !== key) { |
||
108 | return true; |
||
109 | } |
||
110 | } |
||
111 | return false; |
||
112 | }).map(value => value.substr(1)), |
||
113 | ), |
||
114 | ]; |
||
115 | }); |
||
116 | |||
117 | Object.keys(findExtendedGrants).forEach((key) => { |
||
118 | const grant = findExtendedGrants[key]; |
||
119 | |||
120 | grant.forEach((value) => { |
||
121 | result[key] = [...new Set([...result[key], ...result[value]])]; |
||
122 | }); |
||
123 | |||
124 | }); |
||
125 | |||
126 | Object.keys(result).forEach((key) => { |
||
127 | const grant = result[key]; |
||
128 | |||
129 | const per = []; |
||
130 | grant.forEach((value) => { |
||
131 | if (!value.includes('@')) { |
||
132 | per.push(...permissions[value].map((dd) => { |
||
133 | return `${value}@${dd}`; |
||
134 | })); |
||
135 | } |
||
136 | }); |
||
137 | |||
138 | result[key] = [...new Set([...result[key], ...per])]; |
||
139 | |||
140 | }); |
||
141 | |||
142 | if (this.cache) { |
||
143 | this.setIntoCache(result); |
||
144 | } |
||
145 | |||
146 | return result; |
||
147 | } |
||
157 |